package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.sc.nft.dao.BusinessCardConfigDao;
import com.sc.nft.dao.BusinessCardHoldDao;
import com.sc.nft.dao.DigitalCollectionDao;
import com.sc.nft.dao.UserCollectionDao;
import com.sc.nft.entity.BusinessCardConfig;
import com.sc.nft.entity.BusinessCardHold;
import com.sc.nft.entity.DigitalCollection;
import com.sc.nft.entity.dto.BusinessCardSetDTO;
import com.sc.nft.entity.vo.BusinessCardConfirmVO;
import com.sc.nft.entity.vo.BusinessCardSetVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.BusinessCardConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangchao
 * @description 针对表【sc_business_card_config(名片配置)】的数据库操作Service实现
 * @createDate 2024-07-08 18:29:57
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class BusinessCardConfigServiceImpl implements BusinessCardConfigService {

    private final BusinessCardConfigDao businessCardConfigDao;
    private final BusinessCardHoldDao businessCardHoldDao;
    private final DigitalCollectionDao digitalCollectionDao;
    private final UserCollectionDao userCollectionDao;

    @Override
    @Transactional
    public void businessCardSet(BusinessCardSetDTO businessCardSetDTO) {
        //查询信息是否存在
        BusinessCardConfig queryResult = businessCardConfigDao.getBusinessCardConfig();
        BusinessCardConfig businessCardConfig = new BusinessCardConfig();
        BeanUtil.copyProperties(businessCardSetDTO, businessCardConfig);
        businessCardConfig.setId(queryResult == null ? null : queryResult.getId());
        businessCardConfigDao.saveOrUpdate(businessCardConfig);
        //删除配置的藏品
        businessCardHoldDao.removeByConfigId(businessCardConfig.getId());
        //设置藏品呢
        if (businessCardSetDTO.getAppoint() && businessCardSetDTO.getCollectionIdList() != null && businessCardSetDTO.getCollectionIdList().size() > 0) {
            List<BusinessCardHold> businessCardHoldList = new ArrayList<>();
            for (Long collectionId : businessCardSetDTO.getCollectionIdList()) {
                BusinessCardHold businessCardHold = new BusinessCardHold();
                businessCardHold.setConfigId(businessCardConfig.getId());
                businessCardHold.setCollectionId(collectionId);
                businessCardHoldList.add(businessCardHold);
            }
            businessCardHoldDao.saveBatch(businessCardHoldList);
        }
    }

    @Override
    public BusinessCardConfirmVO businessCardConfirm(Long userId) {
        BusinessCardConfirmVO businessCardConfirmVO = new BusinessCardConfirmVO();
        // 获取用户名片配置
        BusinessCardConfig businessCardConfig = businessCardConfigDao.getBusinessCardConfig();
        if (BeanUtil.isEmpty(businessCardConfig)) {
            throw new GlobalRunTimeException("名片配置不存在");
        }
        // 判定是否需要查询
        if (!businessCardConfig.getAppoint()) {
          // 不需要的话直接返回结果
            businessCardConfirmVO.setPrice(businessCardConfig.getPrice());
            businessCardConfirmVO.setIsOpen(businessCardConfig.getAppoint());
            businessCardConfirmVO.setIsHold(Boolean.TRUE);
            return businessCardConfirmVO;
        }
        // 获取配置
        List<BusinessCardHold> configId = businessCardHoldDao.getConfigId(businessCardConfig.getId());
        if (CollUtil.isEmpty(configId)) {
            throw new GlobalRunTimeException("名片配置不存在");
        }
        BusinessCardHold businessCardHold = configId.get(0);
        DigitalCollection digitalCollection = digitalCollectionDao.getById(businessCardHold.getCollectionId());
        businessCardConfirmVO.setCollectionName(digitalCollection.getFirstTitle());
        businessCardConfirmVO.setCollectionImgUrl(digitalCollection.getCreationContent());
        businessCardConfirmVO.setPrice(businessCardConfig.getPrice());
        businessCardConfirmVO.setIsOpen(businessCardConfig.getAppoint());
        businessCardConfirmVO.setIsHold(userCollectionDao.userHasCollection(userId, businessCardHold.getCollectionId()));
        return businessCardConfirmVO;
    }

    @Override
    public BusinessCardSetVO businessCardGet() {
        BusinessCardSetVO businessCardSetVO = businessCardConfigDao.businessCardGet();
        if (BeanUtil.isEmpty(businessCardSetVO)) {
            return new BusinessCardSetVO();
        }
        // 根据是否需要指定藏品来查询
        if (businessCardSetVO.getAppoint()) {
            // 查询指定的藏品
            List<BusinessCardHold> configId = businessCardHoldDao.getConfigId(businessCardSetVO.getId());
            if (CollUtil.isNotEmpty(configId)) {
                BusinessCardHold businessCardHold = configId.get(0);
                DigitalCollection digitalCollection = digitalCollectionDao.getById(businessCardHold.getCollectionId());
                businessCardSetVO.setFirstTitle(digitalCollection.getFirstTitle());
                businessCardSetVO.setCollectionId(digitalCollection.getId());
            }

        }
        return businessCardSetVO;
    }
}




